<?php

namespace app\command;

use app\common\model\GoodsList;
use app\common\model\Order;
use app\common\model\OrderList;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;


class Zd extends Command
{
    protected function configure()
    {
        $this->setName('Zd')->setDescription('自动发货');
    }

    protected function execute(Input $input, Output $output)
    {
    
        //微信官方发货
        $wxServer = new \app\common\server\Wx($this->app);
        $access_token = $wxServer->get_access_token();
        
        $data1 = Db::name('order')->field('id,user_id,order_num,zdfh_is,zdfh_time,addtime')->where('zdfh_is','in',[0,1])->where('status',1)->where('price','>',0)->select()->toArray();
        if($data1){
            foreach ($data1 as $value1){
                if($value1['zdfh_is'] == 0 && (time()-180) > $value1['addtime']){
                    Db::name('order')->where('id',$value1['id'])->update(['zdfh_is'=>1,'zdfh_time'=>(time()+300)]);
                    $user_id = $value1['user_id'];
                    $order_num = $value1['order_num'];
                    $open_id = Db::name('user')->where('id',$user_id)->value('openid');
                    $wxServer->post_order_one($open_id,$access_token,$order_num);    
                }elseif($value1['zdfh_is'] == 1 && time() > $value1['zdfh_time']){
                    Db::name('order')->where('id',$value1['id'])->update(['zdfh_is'=>2]);
                    $user_id = $value1['user_id'];
                    $order_num = $value1['order_num'];
                    $open_id = Db::name('user')->where('id',$user_id)->value('openid');
                    $wxServer->post_order_one($open_id,$access_token,$order_num);
                }
                
            }
        }
        
        $data2 = Db::name('market_order')->field('id,user_id,order_num,zdfh_is,zdfh_time,addtime')->where('zdfh_is','in',[0,1])->select()->toArray();
        if($data2){
            foreach ($data2 as $value2){
                if($value2['zdfh_is'] == 0  && (time()-180) > $value2['addtime']){
                    Db::name('market_order')->where('id',$value2['id'])->update(['zdfh_is'=>1,'zdfh_time'=>(time()+300)]);
                    $user_id = $value2['user_id'];
                    $order_num = $value2['order_num'];
                    $open_id = Db::name('user')->where('id',$user_id)->value('openid');
                    $wxServer->post_order_one($open_id,$access_token,$order_num);
                }elseif($value2['zdfh_is'] == 1 && time() > $value2['zdfh_time']){
                    Db::name('market_order')->where('id',$value2['id'])->update(['zdfh_is'=>2]);
                    $user_id = $value2['user_id'];
                    $order_num = $value2['order_num'];
                    $open_id = Db::name('user')->where('id',$user_id)->value('openid');
                    $wxServer->post_order_one($open_id,$access_token,$order_num);
                }
                
            }
        }
        
        
        // $data3 = Db::name('order_list_send')->field('id,user_id,send_num,zdfh_is,zdfh_time,addtime')->where('zdfh_is','in',[0,1])->select()->toArray();
        // if($data3){
        //     foreach ($data3 as $value3){
        //         if($value3['zdfh_is'] == 0  && (time()-180) > $value3['addtime']){
        //             Db::name('order_list_send')->where('id',$value3['id'])->update(['zdfh_is'=>1,'zdfh_time'=>(time()+300)]);
        //             $user_id = $value3['user_id'];
        //             $order_num = $value3['send_num'];
        //             $open_id = Db::name('user')->where('id',$user_id)->value('openid');
        //             $wxServer->post_order_one($open_id,$access_token,$order_num);
        //         }elseif($value3['zdfh_is'] == 1 && time() > $value3['zdfh_time']){
        //             Db::name('order_list_send')->where('id',$value3['id'])->update(['zdfh_is'=>2]);
        //             $user_id = $value3['user_id'];
        //             $order_num = $value3['send_num'];
        //             $open_id = Db::name('user')->where('id',$user_id)->value('openid');
        //             $wxServer->post_order_one($open_id,$access_token,$order_num);
        //         }
                
        //     }
        // }
        
        
    }


}
